 <?php 

include("../db/opendb.php");
include("../common/phpmailer/class.phpmailer.php");
include('../common/clsxmlparser.php');

$mail = new PHPMailer();
$fields = '';

$mail->From     = "homevalueresults@realivent.com";
$mail->FromName = "List manager";
$mail->Host     = "mail.realivent.com";
$mail->Mailer   = "smtp";

$query = "SELECT g.*, u.Email FROM zillow_guesses g inner join Users u on u.Username = g.user_id where future_date < current_date() and status = 'pending'";
$result = mysql_query($query) or die ("SQL Error".mysql_error());
while($row = mysql_fetch_assoc($result))
{
$p =& new xmlParser();
$url = "http://www.zillow.com/webservice/GetZestimate.htm?zws-id=X1-ZWz1ckurjgj3m3_7nbg7&zpid=".$row['zid'];
$listdata = getContents($url, $fields);
$p->parse($listdata);
$opt = $p->output;
$listings = cma_get_array($opt);

$zestimate = $listings[2][ZESTIMATE][0][content];
$diff_zest = $zestimate-$row['future_price'];
$percent_diff = $diff_zest/$zestimate;

//echo $percent_diff;

$query2 = "UPDATE zillow_guesses set status = 'complete', future_zestimate = '".$zestimate."', price_difference = '".$diff_zest."', percent_diff = '".$percent_diff."' WHERE ID = '".$row[ID]."'";
$result2 = mysql_query($query2) or die ("SQL Error".mysql_error());
//echo $row['Email']."<BR>";
//$mail->AddAddress("matt@realivent.com");
//$mail->Subject = "Here is the subject";
//$mail->Body  = "This is the message body";
//$mail->Send();
}

function getContents($url,$fields) {
	$ch = curl_init($url); 
	$sendheaders = array("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",);
	curl_setopt($ch, CURLOPT_POST, 0);
	curl_setopt($ch, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
	curl_setopt($ch, CURLOPT_NOBODY, 0); // set to 1 to eliminate body info from response
	curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); // use HTTP/1.0 instead of 1.1
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response. ###
	curl_setopt($ch, CURLOPT_HTTPHEADER, $sendheaders);
	//curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); // use HTTP POST to send form data
	
	$resp = curl_exec($ch); //execute post and get results
	curl_close ($ch); 
	return $resp;

}

function cma_get_array($opt) {
 	 $entries = $opt[0][child];
	 $listingsarray = array();
	 
  for($i=0;$i<sizeof($entries);$i++) {
  $entry = $entries[$i][child];
	$listingsarray[$i]=array();
  	for($j=0;$j<sizeof($entry);$j++)
	{
		$item = $entry[$j];
		
		
		
		if(is_array($item[child]))
			$listingsarray[$i][$item[name]] = $item[child];
		else
			$listingsarray[$i][$item[name]] = $item[content];
			
		
			
	
	}	
	
  }
  return $listingsarray;
}

?>

